package com.sgs.carbon.web.modules.common.dal.mapper.dept;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sgs.carbon.starter.mybatis.core.mapper.BaseMapperX;
import com.sgs.carbon.starter.mybatis.core.query.QueryWrapperX;
import com.sgs.carbon.web.modules.common.controller.dept.vo.SysDeptListReqVO;
import com.sgs.carbon.web.modules.common.dal.dataobject.dept.SysDeptDO;

import java.util.Date;
import java.util.List;

/**
 * 部门 Mapper
 *
 * @author Dave Liu
 * @date 2021-10-07
 */
public interface SysDeptMapper extends BaseMapperX<SysDeptDO> {

    default List<SysDeptDO> selectList(SysDeptListReqVO reqVO) {
        return selectList(new QueryWrapperX<SysDeptDO>().likeIfPresent("name", reqVO.getName())
                .eqIfPresent("status", reqVO.getStatus()));
    }

    default SysDeptDO selectByParentIdAndName(Long parentId, String name) {
        return selectOne(new QueryWrapper<SysDeptDO>().eq("parent_id", parentId)
                .eq("name", name));
    }

    default Integer selectCountByParentId(Long parentId) {
        return selectCount(new QueryWrapper<SysDeptDO>().eq("parent_id", parentId));
    }

    default boolean selectExistsByUpdateTimeAfter(Date maxUpdateTime) {
        return selectOne(new QueryWrapper<SysDeptDO>().select("id")
                .gt("update_time", maxUpdateTime).last("LIMIT 1")) != null;
    }

}
